package com.localjoycommunity.cpdj.project.system.service.impl;

import cn.hutool.core.lang.func.Func;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.localjoycommunity.cpdj.common.utils.DateUtils;
import com.localjoycommunity.cpdj.project.system.domain.SysCourse;
import com.localjoycommunity.cpdj.project.system.domain.UserCourseWatch;
import com.localjoycommunity.cpdj.project.system.mapper.SysCourseMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserCourseMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserCourseWatchMapper;
import com.localjoycommunity.cpdj.project.system.service.ISysCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程管理Service业务层处理
 *
 * @author leepandar
 * @date 2020-11-12
 */
@Service
public class SysCourseServiceImpl implements ISysCourseService {
    @Autowired
    private SysCourseMapper sysCourseMapper;
    @Autowired
    private UserCourseWatchMapper userCourseWatchMapper;

    /**
     * 查询课程管理
     *
     * @param id 课程管理ID
     * @return 课程管理
     */
    @Override
    public SysCourse selectSysCourseById(Long id) {
        return sysCourseMapper.selectSysCourseById(id);
    }

    /**
     * 查询课程管理
     *
     * @param id 课程管理ID
     * @return 课程管理
     */
    @Override
    public SysCourse selectSysCourseByIdApp(Long id) {
        SysCourse sysCourse = sysCourseMapper.selectSysCourseById(id);
        sysCourse.setWatchNum(sysCourse.getWatchNum()+1);
        sysCourseMapper.updateSysCourse(sysCourse);
        return sysCourse;
    }

    /**
     * 查询课程管理列表
     *
     * @param sysCourse 课程管理
     * @return 课程管理
     */
    @Override
    public List<SysCourse> selectSysCourseList1(SysCourse sysCourse) {
        List<SysCourse> list =  sysCourseMapper.selectSysCourseList1(sysCourse);
        list.forEach(course -> {
            LambdaQueryWrapper<UserCourseWatch> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserCourseWatch::getCourseId,course.getId());
            List<UserCourseWatch> userCourseWatchList = userCourseWatchMapper.selectList(wrapper);
            if(userCourseWatchList!=null&&userCourseWatchList.size()>0){
                course.setWatchPeople(Long.parseLong(String.valueOf(userCourseWatchList.size())));
            }else{
                course.setWatchPeople(0L);
            }
        });
        return list;
    }
    @Override
    public List<SysCourse> selectSysCourseList(SysCourse sysCourse) {
        List<SysCourse> list =  sysCourseMapper.selectSysCourseList(sysCourse);
        list.forEach(course -> {
            LambdaQueryWrapper<UserCourseWatch> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserCourseWatch::getCourseId,course.getId());
            List<UserCourseWatch> userCourseWatchList = userCourseWatchMapper.selectList(wrapper);
            if(userCourseWatchList!=null&&userCourseWatchList.size()>0){
                course.setWatchPeople(Long.parseLong(String.valueOf(userCourseWatchList.size())));
            }else{
                course.setWatchPeople(0L);
            }
        });
        return list;
    }

    /**
     * 新增课程管理
     *
     * @param sysCourse 课程管理
     * @return 结果
     */
    @Override
    public int insertSysCourse(SysCourse sysCourse) {
        sysCourse.setCreateTime(DateUtils.getNowDate());
        return sysCourseMapper.insertSysCourse(sysCourse);
    }

    /**
     * 修改课程管理
     *
     * @param sysCourse 课程管理
     * @return 结果
     */
    @Override
    public int updateSysCourse(SysCourse sysCourse) {
        return sysCourseMapper.updateSysCourse(sysCourse);
    }

    /**
     * 批量删除课程管理
     *
     * @param ids 需要删除的课程管理ID
     * @return 结果
     */
    @Override
    public int deleteSysCourseByIds(Long[] ids) {
        return sysCourseMapper.deleteSysCourseByIds(ids);
    }

    /**
     * 删除课程管理信息
     *
     * @param id 课程管理ID
     * @return 结果
     */
    @Override
    public int deleteSysCourseById(Long id) {
        return sysCourseMapper.deleteSysCourseById(id);
    }

    /**
     * 增加收藏数
     *
     * @param id
     * @param num
     */
    @Override
    public int updateDigNum(long id, int num) {
        return sysCourseMapper.updateDigNum(id, num);
    }
}
